import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Param,
  Body,
  UseGuards,
  HttpCode,
  Query,
  UploadedFile,
  UseInterceptors,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiParam,
  ApiQuery,
  ApiConsumes,
  ApiBody,
} from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { SingerService } from './singer.service';
import {
  CreateSingerDto,
  UpdateSingerDto,
  SingerResponseDto,
  SingersPaginationQueryDto,
} from './singer.dto';
import { Song } from '../song/song.entity';
import { PaginationResponseDto } from '../song/song.dto';
import { FileInterceptor } from '@nestjs/platform-express';
import { imageMulterConfig } from '../common/config/multer.config';
import { AdminGuard } from '../admin/admin.guard';

@ApiTags('歌手')
@Controller('singers')
export class SingerController {
  constructor(private readonly singerService: SingerService) {}

  @ApiOperation({ summary: '创建新歌手' })
  @ApiResponse({ status: 201, type: SingerResponseDto })
  @ApiResponse({ status: 409, description: '歌手名称已存在' })
  @UseGuards(AuthGuard('jwt'), AdminGuard)
  @Post()
  async createSinger(
    @Body() createSingerDto: CreateSingerDto,
  ): Promise<SingerResponseDto> {
    return this.singerService.createSinger(createSingerDto);
  }

  @ApiOperation({ summary: '获取所有歌手（支持分页）' })
  @ApiResponse({ status: 200, description: '返回分页的歌手列表' })
  @ApiQuery({ type: SingersPaginationQueryDto })
  @Get()
  async getAllSingers(
    @Query() query: SingersPaginationQueryDto,
  ): Promise<PaginationResponseDto<SingerResponseDto>> {
    return this.singerService.getAllSingers(query);
  }

  @ApiOperation({ summary: '获取单个歌手' })
  @ApiResponse({ status: 200, type: SingerResponseDto })
  @ApiResponse({ status: 404, description: '歌手不存在' })
  @ApiParam({ name: 'id', description: '歌手ID' })
  @Get(':id')
  async getSingerById(@Param('id') id: number): Promise<SingerResponseDto> {
    return this.singerService.getSingerById(id);
  }

  @ApiOperation({ summary: '更新歌手' })
  @ApiResponse({ status: 200, type: SingerResponseDto })
  @ApiResponse({ status: 404, description: '歌手不存在' })
  @ApiResponse({ status: 409, description: '歌手名称已存在' })
  @ApiParam({ name: 'id', description: '歌手ID' })
  @UseGuards(AuthGuard('jwt'), AdminGuard)
  @Put(':id')
  async updateSinger(
    @Param('id') id: number,
    @Body() updateSingerDto: UpdateSingerDto,
  ): Promise<SingerResponseDto> {
    return this.singerService.updateSinger(id, updateSingerDto);
  }

  @ApiOperation({ summary: '上传歌手头像' })
  @ApiResponse({ status: 200, description: '头像上传成功' })
  @ApiResponse({ status: 404, description: '歌手不存在' })
  @ApiResponse({ status: 400, description: '文件上传失败' })
  @ApiParam({ name: 'id', description: '歌手ID' })
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        file: {
          type: 'string',
          format: 'binary',
        },
      },
    },
  })
  @UseGuards(AuthGuard('jwt'), AdminGuard)
  @Post(':id/avatar')
  @UseInterceptors(FileInterceptor('file', imageMulterConfig))
  async uploadSingerAvatar(
    @Param('id') id: number,
    @UploadedFile() file: Express.Multer.File,
  ): Promise<{ avatarUrl: string }> {
    console.log(file, id);
    return this.singerService.uploadSingerAvatar(id, file);
  }

  @ApiOperation({ summary: '删除歌手' })
  @ApiResponse({ status: 204, description: '删除成功' })
  @ApiResponse({ status: 404, description: '歌手不存在' })
  @ApiParam({ name: 'id', description: '歌手ID' })
  @UseGuards(AuthGuard('jwt'))
  @HttpCode(204)
  @Delete(':id')
  async deleteSinger(@Param('id') id: number): Promise<void> {
    return this.singerService.deleteSinger(id);
  }

  @ApiOperation({ summary: '获取歌手的所有歌曲' })
  @ApiResponse({ status: 200, type: [Song] })
  @ApiResponse({ status: 404, description: '歌手不存在' })
  @ApiParam({ name: 'id', description: '歌手ID' })
  @Get(':id/songs')
  async getSongsBySinger(@Param('id') id: number): Promise<Song[]> {
    return this.singerService.getSongsBySinger(id);
  }

  @ApiOperation({ summary: '搜索歌手' })
  @ApiResponse({ status: 200, type: [SingerResponseDto] })
  @ApiQuery({ name: 'keyword', description: '搜索关键词' })
  @Get('search')
  async searchSingers(
    @Query('keyword') keyword: string,
  ): Promise<SingerResponseDto[]> {
    return this.singerService.searchSingers(keyword);
  }
}
